home *** CD-ROM | disk | FTP | other *** search
- /*
- * @(#)Servlet.cpp 1.7 97/05/14
- *
- * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
- *
- * This software is the confidential and proprietary information of Sun
- * Microsystems, Inc. ("Confidential Information"). You shall not
- * disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Sun.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
- * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- *
- * CopyrightVersion 1.0
- */
-
- #include <stdio.h>
- #include <HttpExt.h>
- #include <jni.h>
-
- #include <io.h>
-
- #undef AFX_DATA
- #define AFX_DATA AFX_EXT_DATA
-
- #define BUFLEN 1024
-
- //static JDK1_1InitArgs vmArgs;
- static JavaVM *vmPtr;
- static JNIEnv *env = NULL;
-
- static char PreAmble[] = "<body><h1>";
- static char PostAmble[] = "</h1></body>";
-
- char *CLASSPATH, *MAINCLASS, *PATH, *PROPFILE;
-
- int getRegistryValues();
-
- void clobberClassPath(JDK1_1InitArgs *args) {
- putenv("CLASSPATH=");
- putenv("JAVA_HOME=");
- putenv(PATH);
- args->classpath = CLASSPATH;
- }
-
- DWORD SendError( EXTENSION_CONTROL_BLOCK *pEcb, const char *msg, DWORD status )
- {
- char buf[BUFLEN];
- int preLen = strlen(PreAmble);
- int postLen = strlen(PostAmble);
- int msgLen = strlen(msg);
- strcpy(pEcb->lpszLogData, msg);
- pEcb->dwHttpStatusCode = status;
- strcpy(buf, PreAmble);
- int toWrite = (msgLen > BUFLEN - preLen - postLen) ?
- BUFLEN - preLen - postLen : msgLen;
-
- strncpy(buf+preLen, msg, toWrite);
- strcpy(buf+preLen+msgLen, PostAmble);
- DWORD len = strlen(buf);
-
- pEcb->WriteClient(pEcb->ConnID, buf, &len, HSE_IO_SYNC);
-
- return HSE_STATUS_SUCCESS;
- }
-
- BOOL WINAPI GetExtensionVersion( HSE_VERSION_INFO *pVer )
- {
- JDK1_1InitArgs vmArgs;
- jclass mainClass;
-
- pVer->dwExtensionVersion = MAKELONG( HSE_VERSION_MINOR,
- HSE_VERSION_MAJOR );
- lstrcpyn( pVer->lpszExtensionDesc,
- "Java Servlet Runner",
- HSE_MAX_EXT_DLL_NAME_LEN );
-
- if (env != NULL) {
- return TRUE;
- }
-
- getRegistryValues();
-
- JNI_GetDefaultJavaVMInitArgs(&vmArgs);
-
- // clobber the classpath
- clobberClassPath(&vmArgs);
-
- // create the VM
- if (JNI_CreateJavaVM(&vmPtr, &env, &vmArgs)) {
- return FALSE;
- }
-
- // find the main class
- mainClass = env->FindClass(MAINCLASS);
- if (mainClass == NULL) {
- return FALSE;
- }
-
- // find the static void main(String[] arg) method
- jmethodID mID = env->GetStaticMethodID(mainClass,
- "main",
- "([Ljava/lang/String;)V");
- if (mID == NULL) {
- return FALSE;
- }
-
- // execute the main method
- jclass strClass = env->FindClass("java/lang/String");
- jobjectArray jargs = env->NewObjectArray(1, strClass, 0);
- char buf[256];
- sprintf(buf, "servlet.propfile=\0");
- strcat(buf, PROPFILE);
- jstring str = env->NewStringUTF(buf);
- env->SetObjectArrayElement(jargs, 0, str);
- env->CallStaticVoidMethod(mainClass, mID, jargs);
-
- jthrowable exc = env->ExceptionOccurred();
- if (exc) {
- return FALSE;
- }
-
- return TRUE;
- }
-
- DWORD WINAPI HttpExtensionProc( EXTENSION_CONTROL_BLOCK *pEcb )
- {
- jclass mainClass;
- jmethodID serviceMethod;
-
- // find the main class
- mainClass = env->FindClass(MAINCLASS);
- if (mainClass == NULL) {
- return HSE_STATUS_ERROR;
- }
-
- // find the static void handleConnection(long) method
- serviceMethod = env->GetStaticMethodID(mainClass,
- "handleConnection",
- "(J)V");
-
- if (serviceMethod == NULL) {
- return HSE_STATUS_ERROR;
- }
-
- //jstring ret = (jstring)env->CallStaticObjectMethod(mainClass, serviceMethod, pEcb);
- //const char *bbb = env->GetStringUTFChars(ret, 0);
- //env->ReleaseStringUTFChars(ret, bbb);
-
- env->CallStaticVoidMethod(mainClass, serviceMethod, pEcb);
-
- jthrowable exc = env->ExceptionOccurred();
- if (exc) {
- return HSE_STATUS_ERROR;
- }
- return HSE_STATUS_SUCCESS;
- }
-
-
- #undef AFX_DATA
- #define AFX_DATA
-